package net.jxtz.jxtzos.authentication;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 退出登陆成功处理器
 * @author a123
 */
@Slf4j
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {

    @Value("${cross-path}")
    private String crossPath;

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        String s = "退出成功";
        log.info(s);
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", crossPath);
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Headers","*");
        httpServletResponse.setHeader("Access-Control-Allow-Credentials","true");
        httpServletResponse.setHeader("Access-Control-Expose-Headers", "*");
        httpServletResponse.setStatus(200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", s);
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write(JSONObject.toJSONString(map));
        writer.flush();
        writer.close();
    }
}
